<style>
  .layui-input-inline {
    width: 33% !important;
  }
  .layui-card .layui-tab-brief .layui-tab-title li{
    font-size: 18px;
    font-weight: 600;
    line-height: 46px;
  }
  @media screen and (max-width: 768px) {
    .layui-input-inline {
      width: 60% !important;
    }
  }
</style>
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>数据分析</cite></a>
    <a><cite>修复统计数据</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">平台数据</li>
      </ul>
      <div class="layui-tab-content">
        <form class="layui-form">
          <div class="layui-form-item" style="margin:0;">
            <label class="layui-form-label">统计日期</label>
            <div class="layui-input-inline">
              <input type="text" name="statisticsDate" lay-verify="required" placeholder="请输入统计日期" autocomplete="off" class="layui-input">
              <div style="color:red;font-size: 12px;">* 日期不能为今天，格式参考：2021-01-01</div>
            </div>
            <div class="layui-input-inline">
              <button class="layui-btn" lay-submit lay-filter="platData"><i class="layui-icon layui-icon-refresh"></i>重新统计</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>

  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">商户数据</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-form">
          <div class="layui-form-item">
            <label class="layui-form-label">统计日期</label>
            <div class="layui-input-inline">
              <input type="text" name="statisticsDate" lay-verify="required" placeholder="请输入统计日期" autocomplete="off" class="layui-input">
              <div style="color:red;font-size: 12px;">* 日期不能为今天，格式参考：2021-01-01</div>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">商户ID</label>
            <div class="layui-input-inline">
              <input type="text" name="mchId" placeholder="请输入商户ID" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
              <button class="layui-btn" lay-submit lay-filter="mchData"><i class="layui-icon layui-icon-refresh"></i>重新统计</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">支付通道数据</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-form">
          <div class="layui-form-item">
            <label class="layui-form-label">统计日期</label>
            <div class="layui-input-inline">
              <input type="text" name="statisticsDate" lay-verify="required" placeholder="请输入统计日期" autocomplete="off" class="layui-input">
              <div style="color:red;font-size: 12px;">* 日期不能为今天，格式参考：2021-01-01</div>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">支付通道ID</label>
            <div class="layui-input-inline">
              <input type="text" name="payPassageId" placeholder="请输入支付通道ID" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
              <button class="layui-btn" lay-submit lay-filter="payPassageData"><i class="layui-icon layui-icon-refresh"></i>重新统计</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script>
    layui.use(['admin', 'util', 'form'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,element = layui.element
            ,form = layui.form;

        element.render('breadcrumb', 'breadcrumb');

        //平台重新统计
        form.on('submit(platData)', function(data) {
          dataReq(data, "/data/plat/correct");
          return false;
        });

      //商户重新统计
      form.on('submit(mchData)', function(data) {
        dataReq(data, "/data/mch/correct");
        return false;
      });

      //支付通道重新统计
      form.on('submit(payPassageData)', function(data) {
        dataReq(data, "/data/passage/correct");
        return false;
      });

        var dataReq = function(data, reqUrl) {
          data.field.statisticsDate = data.field.statisticsDate.trim();
          var reg = /^(\d{1,4})(-|\/)(\d{2})\2(\d{2})$/;
          if (!reg.test(data.field.statisticsDate) && data.field.statisticsDate.indexOf("#") == -1) {
            layer.alert("日期格式错误！");
            return false;
          }

          layer.confirm('确定统计 <span style="color: red">' + data.field.statisticsDate + " </span>的数据吗？", function(index){
            layer.close(index);
            var load = layer.msg('正在统计，请耐心等待，不要刷新页面...', {
              icon: 16
              ,shade: 0.3
              ,time: 9999999999
            });
            var startTime = Date.now();
            admin.req({
              type: 'post',
              url: layui.setter.baseUrl + reqUrl,
              data: data.field,
              success: function(res) {
                layer.close(load);
                if(res.code == "0"){
                  var endTime = Date.now();
                  var usedTime = endTime - startTime;  //两个时间戳相差的毫秒数
                  //计算出小时数
                  var hours=Math.floor(usedTime/(3600*1000));
                  //计算相差分钟数
                  var leave1=usedTime%(3600*1000);        //计算小时数后剩余的毫秒数
                  var minutes=Math.floor(leave1/(60*1000));
                  var leave2=leave1%(60*1000);        //计算分钟数后剩余的毫秒数
                  var seconds=parseFloat(leave2/1000).toFixed(3);
                  if (hours > 0) {
                    layer.alert("统计完成，耗时："+hours+"时"+minutes+"分"+seconds+"秒");
                  }else if (!hours && minutes > 0 ) {
                    layer.alert("统计完成，耗时："+minutes+"分"+seconds+"秒");
                  }else if (!hours && !minutes && seconds > 0 ) {
                    layer.alert("统计完成，耗时："+seconds+"秒");
                  }
                }
              },complete: function () {
                layer.close(load);
              }
            });
          });
        };

        // 渲染表单
        form.render();
    });
</script>